package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.entity.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao ordersettingDao;
    @Override
    public void insertOrderSettingFile(MultipartFile excelFile) {
        //在内存中创建一个Excel文件对象
        OrderSetting orderSetting = null;
        try {
            XSSFWorkbook excel = new XSSFWorkbook(excelFile.getInputStream());
            XSSFSheet sheet = excel.getSheetAt(0);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {
                Date orderDate = sheet.getRow(i).getCell(0).getDateCellValue();
//                Date parse = sdf.parse(orderDate);
                //返回的是一个double类型，直接int强转就行，不要转换成包装类，不然不好转成int
                double numericCellValue = sheet.getRow(i).getCell(1).getNumericCellValue();
                int number = (int) numericCellValue;
                orderSetting = new OrderSetting(orderDate,number);
                ordersettingDao.insertOrderSetting(orderSetting);
            }


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    @Override
    public List<OrderSetting> getOrderSettingByMonth(String year, String month) {
        YearMonth yearMonth = YearMonth.of(Integer.parseInt(year), Integer.parseInt(month));
        LocalDate start = yearMonth.atDay(1);
        LocalDate end = yearMonth.atEndOfMonth();
        List<OrderSetting> orderSettings = ordersettingDao.getOrderSettingByMonth(start, end);
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            LocalDate localDate = orderDate.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
            int day = localDate.getDayOfMonth();
            orderSetting.setDate(day);
            orderSetting.setReservations(ordersettingDao.selectOrderedBYTempDate(localDate));
        }
        return orderSettings;

    }

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        LocalDate orderDate = orderSetting.getOrderDate().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();

        ordersettingDao.deleteByDate(orderDate);
        ordersettingDao.editNumberByDate(orderDate,orderSetting.getNumber());
    }


}
